package exercise.jslide.jslide.model;

import java.io.Serializable;

import exercise.jslide.jslide.utils.SerializableColor;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;

/**
 * <p>{@code SlideElement} 类是一个抽象类，用于表示幻灯片中的元素。
 * 一个幻灯片元素可以是文本框、图片、形状等。
 * 该类实现了 {@code Serializable} 接口，可以被序列化。</p>
 * <p>该类包含了幻灯片元素的 ID、位置、大小、是否被选中、旋转角度等基本元素属性。</p>
 * <p>该类提供了绘制幻灯片元素、判断鼠标点击位置是否在元素内、克隆元素等操作元素的方法。</p>
 */
public abstract class SlideElement implements Serializable {
    /**
     * 元素的唯一标识符，使用 {@code UUID} 自动生成。
     */
    protected String id;
    /**
     * 元素的x坐标
     */
    protected double x;
    /**
     * 元素的y坐标
     */
    protected double y;
    protected double currentX;
    protected double currentY;
    protected double width;
    protected double height;
    protected boolean selected;
    protected double rotation;
    protected boolean isEditing;
    //颜色
    protected SerializableColor fillColor;
    protected SerializableColor strokeColor;

    public SlideElement() {
        this.id = java.util.UUID.randomUUID().toString();
        this.fillColor = new SerializableColor(javafx.scene.paint.Color.WHITE);
        this.strokeColor = new SerializableColor(javafx.scene.paint.Color.BLACK);
    }

    // 抽象方法
    public abstract void draw(GraphicsContext gc);

    public abstract boolean contains(Point2D point);

    public abstract SlideElement clone();

    // Getter和Setter方法
    public String getId() {
        return id;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public double getRotation() {
        return rotation;
    }

    public void setRotation(double rotation) {
        this.rotation = rotation;
    }

    // 移动元素
    public void move(double deltaX, double deltaY) {
        this.x += deltaX;
        this.y += deltaY;
    }

    // 调整大小
    public void resize(double newWidth, double newHeight) {
        this.width = newWidth;
        this.height = newHeight;
    }

    //设置鼠标点击的位置
    public void setCurrentX(double currentX) {
        this.currentX = currentX;
    }

    public void setCurrentY(double currentY) {
        this.currentY = currentY;
    }

    //获取鼠标点击的位置
    public double getCurrentX() {
        return currentX;
    }

    public double getCurrentY() {
        return currentY;
    }

    public void drawThumbnail(GraphicsContext gc) {
        // 缩略图绘制
        gc.save();
        gc.translate(x, y); // 应用元素的位移
        draw(gc);
        gc.restore();
    }

    public void setEditing(boolean editing) {
        this.isEditing = editing;
    }

    public boolean getEditing() {
        return isEditing;
    }

    public void setFillColor(SerializableColor fillColor) {
        this.fillColor = fillColor;
    }

    public void setStrokeColor(SerializableColor strokeColor) {
        this.strokeColor = strokeColor;
    }

    public SerializableColor getFillColor() {
        return fillColor;
    }

    public SerializableColor getStrokeColor() {
        return strokeColor;
    }
}